package com.leyou.trade.web;

import com.leyou.trade.service.OrderService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("pay")
public class PayController {

    private final OrderService orderService;

    public PayController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 根据订单编号创建支付链接
     * @param orderId 订单编号
     * @return 支付链接
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long orderId){

        return ResponseEntity.ok(orderService.getPayUrl(orderId));
    }

    /**
     * 微信支付成功回调
     * @param data 微信回调的参数
     * @return 成功处理回调后的结果
     */
    @PostMapping(value = "/wx/notify", produces = "application/xml")
    public Map<String,String> hello(@RequestBody Map<String,String> data){

        // 处理回调
//        log.info("[支付回调] 接收微信支付回调, 结果:{}", data);
        orderService.handleNotify(data);
        // 返回成功
        Map<String,String> msg = new HashMap<>();
        msg.put("return_code","SUCCESS");
        msg.put("return_msg","OK");
        return msg;
    }

}
